package org.desktop.web.client.role.window;

import org.desktop.web.client.custom.widget.AbstractWindow;
import org.desktop.web.client.gui.models.AbstractWindowForm.OperationType;
import org.desktop.web.client.images.Resources;
import org.desktop.web.client.role.RoleService;
import org.desktop.web.client.role.form.RoleFormWindow;
import org.desktop.web.client.role.grid.RolesGrid;
import org.desktop.web.client.util.Util;
import org.desktop.web.shared.Response;
import org.desktop.web.shared.domain.entities.Role;

import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.Dialog.PredefinedButton;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
import com.sencha.gxt.widget.core.client.box.ConfirmMessageBox;
import com.sencha.gxt.widget.core.client.box.MessageBox;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.HideEvent;
import com.sencha.gxt.widget.core.client.event.HideEvent.HideHandler;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;

public class RolesWindow extends AbstractWindow {

	FramedPanel panel;
	private RolesGrid rolesGrid;
	
	@Override
	public String id() {
		// TODO Auto-generated method stub
		return "ROLES";
	}

	@Override
	public void init() {

		this.setWidth(790);
		this.setHeight(480);
		this.panel = new FramedPanel();
		this.panel.setHeaderVisible(false);

		this.rolesGrid = new RolesGrid(20);

		TextButton addRole = new TextButton();
		addRole.setIcon(Resources.IMAGES.add());
		addRole.setText("Add role");
		addRole.addSelectHandler(new SelectHandler() {
			
			@Override
			public void onSelect(SelectEvent event) {
				RoleFormWindow roleWindow = new RoleFormWindow(OperationType.INSERT,rolesGrid);
				roleWindow.init();
				roleWindow.setModal(true);
				roleWindow.addHideHandler(new HideHandler() {
					
					@Override
					public void onHide(HideEvent event) {
						// TODO Auto-generated method stub
						// TODO aggiungere evento aggiornamento griglia.  
						
					}
				});
				roleWindow.show();
			}
		});
		TextButton removeRole = new TextButton("Remove");
		removeRole.setIcon(Resources.IMAGES.delete());
		removeRole.addSelectHandler(new SelectHandler() {
			
			@Override
			public void onSelect(SelectEvent event) {
				if(rolesGrid.getGrid().getSelectionModel().getSelectedItem()==null){
					Util.showAlert("Attenzione!!", "Selezionare riga da eliminare", MessageBox.ICONS.warning());
					return;
				}
				ConfirmMessageBox cmb = new ConfirmMessageBox("Cancella", "Sei sicuro di voler rimuovere il ruolo?");
				cmb.setModal(true);
				cmb.setButtonAlign(BoxLayoutPack.CENTER);
				cmb.setPredefinedButtons(PredefinedButton.OK,PredefinedButton.CANCEL);
				
				cmb.getButton(PredefinedButton.OK).addSelectHandler(new SelectHandler() {
					
					@Override
					public void onSelect(SelectEvent event) {
						RoleService.ServiceFactory.createService().deleteRole(Util.getHeaders(),rolesGrid.getGrid().getSelectionModel().getSelectedItem(),new AsyncCallback<Response>() {

							@Override
							public void onFailure(Throwable arg0) {
								Util.showAlert("Error", arg0.getMessage(), MessageBox.ICONS.error());
							}

							@Override
							public void onSuccess(Response response) {
								if(response.getResponseCode()==200){
									Util.showAlert("", "Il record è stato cancellato", MessageBox.ICONS.info());
									rolesGrid.load();
									return;
								}
							}
						});
					}
				});
				cmb.show();
			}
		});
		
		ToolBar rolesToolBar = new ToolBar();
		rolesToolBar.add(addRole);
		rolesToolBar.add(removeRole);


		VerticalLayoutContainer mainPanel = new VerticalLayoutContainer();

		VerticalLayoutContainer roleContainer = new VerticalLayoutContainer();
//		VerticalLayoutContainer subProjectContainer = new VerticalLayoutContainer();

		rolesToolBar.setLayoutData(new VerticalLayoutData(1, -1));
		roleContainer.add(rolesToolBar);
		// BorderLayoutContainer con = new BorderLayoutContainer();

		ContentPanel rolesPanel = new ContentPanel();
		rolesPanel.setHeaderVisible(false);
		rolesPanel.add(rolesGrid.asWidget());

//		ContentPanel subProjectPanel = new ContentPanel();
//		subProjectPanel.setHeaderVisible(false);
//		subProjectPanel.add(spGrid.asWidget());

		// gli eventi sui button della toolbar vanno aggiunti inquesto punto,
		// dopo la chiamata al metodo asWidget

//		final Grid<SubProject> child = spGrid.getGrid();
		final Grid<Role> parent = rolesGrid.getGrid();

		

		addRole.addSelectHandler(new SelectHandler() {

			@Override
			public void onSelect(SelectEvent event) {
//				ProjectFormWindow form = new ProjectFormWindow(pGrid);
//				form.init();
//				form.show();

			}
		});

		removeRole.addSelectHandler(new SelectHandler() {

			@Override
			public void onSelect(SelectEvent event) {
				if (parent.getSelectionModel().getSelectedItem() == null) {
					AlertMessageBox b = new AlertMessageBox("Error",
							"Selezionare la riga da eliminare");
					b.setIcon(AlertMessageBox.ICONS.warning());
					b.setModal(true);
					b.show();
					return;
				}



			}
		});
		VerticalLayoutData vldRolesGrid = new VerticalLayoutData(1.0, 1.0,
				new Margins(5, 0, 5, 0));
//		VerticalLayoutData vldSubProjectGrid = new VerticalLayoutData(1.0, 0.5);

		roleContainer.add(rolesPanel, new VerticalLayoutData(1, 1));
		roleContainer.add(rolesGrid.getPagingToolBar(), new VerticalLayoutData(
				1, -1));
//		subProjectContainer.add(subProjectPanel, new VerticalLayoutData(1, 1));
//		subProjectContainer.add(spGrid.getPagingToolBar(),
//				new VerticalLayoutData(1, -1));

		mainPanel.add(roleContainer, vldRolesGrid);
//		mainPanel.add(subProjectContainer, vldSubProjectGrid);

		this.panel.add(mainPanel);
		this.add(panel);
		// TODO Auto-generated constructor stub
	}

}
